package com.bybops.ui.fragment

import android.os.Bundle
import android.view.View
import baseproject.com.mybaseproject.ui.base.BaseFragment
import baseproject.com.mybaseproject.utils.scheduler.RxUtils
import com.bybops.R
import com.bybops.ui.activity.NewDeviceUseActivity
import com.bybops.utils.BlueToothUtils
import kotlinx.android.synthetic.main.fragment_new_device_test.*

/**
 * 新设备启用的测试界面
 */
class NewDeviceTestFragment : BaseFragment() {


    private var lockStatus = false
    var mActivity: NewDeviceUseActivity? = null

    override fun getLayoutId(): Int? {
        return R.layout.fragment_new_device_test
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mActivity = activity as NewDeviceUseActivity
        initView()
    }

    private fun initView() {
        RxUtils.rxClick(imClickOpenLock, object : RxUtils.OnClickInterf {
            override fun onViewClick(view: View) {
                if (lockStatus) {
                    //下一步
                    showProgress("正在加载...")
                    //判断锁是否已经关闭
                    BlueToothUtils.readLockStatus(mActivity?.lockName, object : BlueToothUtils.LockStatusInf {
                        override fun openLock(isOpenSuccess: Boolean) {
                        }

                        override fun openFail(msg: String?) {
                            showToast(msg!!)
                            hideProgress()
                        }

                        override fun currVoltageAndStatus(voltage: String?, lockStatus: Boolean) {
                            if (lockStatus) {
                                //开的状态
                                showToast("请先关闭锁")
                            } else {
                                //关的状态
                                mActivity?.step(2)
                            }
                            hideProgress()
                        }

                        override fun connectStatus(connected: Boolean) {
                        }
                    })

                } else {
                    //开锁
                    showProgress("正在开锁...")
                    BlueToothUtils.openLock(mActivity?.lockName, object : BlueToothUtils.LockStatusInf {
                        override fun openLock(isOpenSuccess: Boolean) {
                            if (isOpenSuccess) {
                                //更换按钮图标
                                imClickOpenLock.setImageResource(R.mipmap.icon_click_open_lock_next)
                                tvTestDesc.text = "手动关锁后点击下一步"
                                lockStatus = true
                            } else {
                                showToast("未打开锁")
                            }
                            hideProgress()
                        }

                        override fun openFail(msg: String?) {
                            showToast(msg)
                            hideProgress()
                        }

                        override fun currVoltageAndStatus(voltage: String?, lockStatus: Boolean) {

                        }

                        override fun connectStatus(connected: Boolean) {

                        }
                    })
                }
            }
        })
    }
}